;;重新定义逻辑位置：当前目录->当前文件=当前程序->当前函数定义->当前函数调用->当前参数序号
;; _if_add 'cond|'then|'else='body|'para
;; 如果_归并 ('条件|'那么|'否则)=('给定定义|'主体|'参数序号)
(load "cons.lisp")
(load "loc.lisp")
(defvar current-call-function nil)
(defvar current-call-function-num 0)
(defvar 当前目录 ".")
(defvar 当前文件 (concatenate 'string current-function-name ".lisp"))
(defvar 当前程序 l)
(defvar 当前函数定义 `(func-define ,current-function))
(defvar 当前函数调用 `(func-call ,current-call-function))

(defun if-condp(cond-)
  (or (eql cond- 'cond) (eql cond- '条件)))
(defun if-thenp(then-)
  (or (eql then- 'then) (eql then- '那么)))
(defun if-elsep(else-)
  (or (eql else- 'else) (eql else- '否则)))
(defun is-body(body-)
  (or (eql body- 'body) (eql body- '主体)))
(defun is-para(para-)
  (if (atom para-)
      (or (eql para- 'para) (eql para- '参数序号))
      (or (eql (car para-) 'para)(eql (car para-) '参数序号))))
(defun get-para-num(para-)
  (if (listp para-)
      (if (integerp (cadr para-)) (cadr para-) 0) 0))
(defun is-mydefine(par)
  (and (not (is-body par))(not (is-para par))))

(defun get-loc(l loc-type body-num)
  (let ((myl (_loc l loc-type)))
	(if (= body-num 0)
	    (if (> (length (nthcdr 3 myl)) 1)
		(concatenate 'list (list 'progn) (nthcdr 3 myl))
		(nth 3 myl))
	    (nth (+ body-num 2) myl))))

(defun _if_gen(ll loc-type merge-list &optional (body-num 0))
  (let ((len (length merge-list))(cond- (nth 0 (nth 0 merge-list)))(cond-v (nth 1 (nth 0 merge-list)))
	(then- (nth 0 (nth 1 merge-list)))(then-v (nth 1 (nth 1 merge-list)))(else- (nth 0 (nth 2 merge-list)))
	(else-v (nth 1 (nth 2 merge-list))))
    (if (and (> len 0) (< len 4))
	(cond ((and (if-condp cond-)(if-thenp then-)(if-elsep else-))
	       (cond ((and (eql (car loc-type) 'func-define)(is-body cond-v)(is-mydefine then-v)(is-mydefine else-v))
		      (_edit ll loc-type `((body (if ,(get-loc ll loc-type body-num) ,then-v ,else-v) ,body-num))))
		     ((and (eql (car loc-type) 'func-define)(is-body then-v)(is-mydefine cond-v)(is-mydefine else-v))
		      (_edit ll loc-type `((body (if ,cond-v ,(get-loc ll loc-type body-num) ,else-v) ,body-num))))
		     ((and (eql (car loc-type) 'func-define)(is-body else-v)(is-mydefine cond-v)(is-mydefine then-v))
		      (_edit ll loc-type `((body (if ,cond-v ,then-v ,(get-loc ll loc-type body-num)) ,body-num))))
		     (t (format t "如果的归并信息不正确。[if cond then else]~%"))))
	      ((and (if-condp cond-)(if-thenp then-)(null else-)
	       (cond ((and (eql (car loc-type) 'func-define)(is-body cond-v)(is-mydefine then-v))
		      (_edit ll loc-type `((body (if ,(get-loc ll loc-type body-num) ,then-v) ,body-num))))
		     ((and (eql (car loc-type) 'func-define)(is-body then-v)(is-mydefine cond-v))
		      (_edit ll loc-type `((body (if ,cond-v ,(get-loc ll loc-type body-num)) ,body-num))))
		     (t (format t "如果的归并信息不正确。[if cond then]~%")))))
	      
	      (t (format t "如果的归并列表不符合规则。[~a]~%" merge-list)))
	(format t "如果的归并列表元素个数非法。[~a]~%" len))))
